/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.tabbedproperties.section;


import org.eclipse.core.runtime.Assert;
import org.eclipse.gef.editparts.AbstractGraphicalEditPart;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.views.properties.tabbed.ITabbedPropertyConstants;
import org.eclipse.ui.views.properties.tabbed.TabbedPropertySheetPage;

import com.cea.papyrus.core.tabbedproperties.PropertyViewSection;
import com.cea.papyrus.diagraminterchange2.di2.GraphElement;

/**
 * Section to customize mutliplicity display for a property when this property
 * is displayed as a relation end of an association.
 * 
 * @author Remi Schnekenburger
 */
public class PropertyRelationEndMultiplicityAppearenceSection extends PropertyViewSection {
	
	/**
	 * 
	 */
	private GraphElement graphElement;
	
	// there should be only 1 button: show visibility
	/**
	 * 
	 */
	private Button displIsVisible;
	
	/**
	 * 
	 */
	private SelectionListener displIsVisibleListener;

	/* (non-Javadoc)
	 * @see org.eclipse.ui.views.properties.tabbed.AbstractPropertySection#createControls(org.eclipse.swt.widgets.Composite, org.eclipse.ui.views.properties.tabbed.TabbedPropertySheetPage)
	 */
	/**
	 * 
	 * 
	 * @param tabbedPropertySheetPage 
	 * @param parent 
	 */
	public void createControls(Composite parent, TabbedPropertySheetPage tabbedPropertySheetPage) {
		super.createControls(parent, tabbedPropertySheetPage);
		Composite composite = getWidgetFactory().createFlatFormComposite(parent);
		FormData data;

		displIsVisible = getWidgetFactory().createButton(composite, "Visible", SWT.CHECK);
		data = new FormData();
		data.left = new FormAttachment(0, 0);
		data.top = new FormAttachment(0, ITabbedPropertyConstants.HSPACE);
		displIsVisible.setLayoutData(data);
		displIsVisible.addSelectionListener(displIsVisibleListener = new IsVisibleSelectionListener());
	}

	/*
	 * @see org.eclipse.ui.views.properties.tabbed.view.ITabbedPropertySection#refresh()
	 */
	/**
	 * 
	 */
	public void refresh() {
		if(!displIsVisible.isDisposed()){
			
			// if no graph element are associated, there should not be the appearence section
			if(graphElement == null) {
				return;
			}
			// get Property associated to this element
			displIsVisible.setSelection(graphElement.isIsVisible());
		}
	}
	
	/* (non-Javadoc)
	 * @see com.cea.papyrus.core.ui.properties.tabbed.PropertyViewSection#setInput(org.eclipse.ui.IWorkbenchPart, org.eclipse.jface.viewers.ISelection)
	 */
	/**
	 * 
	 * 
	 * @param part 
	 * @param selection 
	 */
	public void setInput(IWorkbenchPart part, ISelection selection) {
		super.setInput(part, selection);
		Assert.isTrue(selection instanceof IStructuredSelection);
		Object input = ((IStructuredSelection) selection).getFirstElement();

		if(input instanceof AbstractGraphicalEditPart) {
			graphElement = ((GraphElement)((AbstractGraphicalEditPart) input).getModel());
		} else {
			graphElement = null;
		}
		refresh();
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.cea.papyrus.core.ui.properties.tabbed.PropertyViewSection#dispose()
	 */
	/**
	 * 
	 */
	public void dispose(){
		super.dispose();
		if(displIsVisible!=null && !displIsVisible.isDisposed())
			displIsVisible.removeSelectionListener(displIsVisibleListener);
	}

	/**
	 * 
	 */
	private class IsVisibleSelectionListener implements SelectionListener {

		/* (non-Javadoc)
		 * @see org.eclipse.swt.events.SelectionListener#widgetDefaultSelected(org.eclipse.swt.events.SelectionEvent)
		 */
		/**
		 * 
		 * 
		 * @param e 
		 */
		public void widgetDefaultSelected(SelectionEvent e) {

		}

		/* (non-Javadoc)
		 * @see org.eclipse.swt.events.SelectionListener#widgetSelected(org.eclipse.swt.events.SelectionEvent)
		 */
		/**
		 * 
		 * 
		 * @param e 
		 */
		public void widgetSelected(SelectionEvent e) {
			Button button = (Button)e.getSource();
			boolean isSelected = button.getSelection();

			if(graphElement == null) {
				return;
			}
			graphElement.setIsVisible(isSelected);
			forceToSave();
			refresh();
		}
	}
}

